package com.bangyo.common.spider.impl;

import java.io.IOException;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.bangyo.common.spider.PatternResolver;
import com.bangyo.common.spider.Spider;

public class VideoEmbedCodeSpider implements Spider<String>
{
    private final Log logger = LogFactory.getLog(VideoEmbedCodeSpider.class);

    private PatternResolver patternResolver = null;

    private int connectTimeout = 0;

    private int maxContentLength = 0;

    private List<String> acceptedContentTypes = null;

    public void setPatternResolver(PatternResolver patternResolver)
    {
        this.patternResolver = patternResolver;
    }

    public String spy(String url) throws IOException
    {
        URL u = new URL(url);
        Pattern pattern = patternResolver.resolve(u);
        if (null==pattern)
        {
            if (logger.isDebugEnabled())
            {
                logger.debug("can not resolve pattern of url["+url+"].");
            }
            return null;
        }
        try
        {
            URLConnection connection = u.openConnection();
            connection.setConnectTimeout(connectTimeout);
            if ((maxContentLength>0&&connection.getContentLength()>maxContentLength))
            {
                if (logger.isWarnEnabled())
                {
                    logger.warn("content length too large of url["+url+"].");
                }
                return null;
            }
            if (null!=acceptedContentTypes)
            {
                String contentType = connection.getContentType();
                boolean match = false;
                if (null!=contentType)
                {
                    contentType = contentType.toLowerCase();
                    for (String type:acceptedContentTypes)
                    {
                        if (contentType.contains(type))
                        {
                            match = true;
                            break;
                        }
                    }
                }
                if (!match)
                {
                    if (logger.isWarnEnabled())
                    {
                        logger.warn("content type["+contentType+"] of url["+url
                            +"] is not accepted .");
                    }
                    return null;
                }
            }
            String content = IOUtils.toString(connection.getInputStream());
            Matcher m = pattern.matcher(content);
            if (m.find())
            {
                return m.group(1);
            }
            if (logger.isWarnEnabled())
            {
                logger.warn("content not match with url=["+url
                    +"] and pattern=["+pattern.pattern()+"].");
            }
        }
        catch (SocketTimeoutException e)
        {
            if (logger.isWarnEnabled())
            {
                logger.warn("timeout when connecting to url["+url+"].");
            }
        }
        return null;
    }

    public void setMaxContentLength(int maxContentLength)
    {
        this.maxContentLength = maxContentLength;
    }

    public void setConnectTimeout(int timeout)
    {
        this.connectTimeout = timeout;
    }

    public void setAcceptedContentTypes(List<String> acceptedContentTypes)
    {
        this.acceptedContentTypes = acceptedContentTypes;
    }

}
